﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;
using University.Workshop.Model;
using System.Text.RegularExpressions;
using System.Globalization;

namespace University.Workshop.Business
{
    public static class RegisterCommentManager
    {
        public static Comment RegisterComment(String email, String text, String topicId)
        {
            if ( String.IsNullOrEmpty(text) || String.IsNullOrEmpty(email))
            {
                throw new ValidationException("All fields are required");
            }
            String format = @"^[a-zA-Z1-9 !?()&]";
            if (!Regex.IsMatch(text, format) || text.Contains('>') || text.Contains('<'))
            {
                throw new ValidationException("Invalid text");
            }
            User user=DataAccess.UserDataAccess.GetUserByEmail(email);
            Comment comment=new Comment();
            comment.Date=DateTime.Now;
            comment.UserId=user.Id;
            comment.TopicId=Convert.ToInt64( topicId,CultureInfo.CurrentCulture);
            comment.Text=text;

            comment=DataAccess.CommentDataAccess.InsertComment(comment);
            return comment;
        }
    }
}
